/**
 * TopView
 * 
 * Version 1.0
 *
 * Nov 29th, 2009
 * 
 * Copyright (c) 2009 Duc Nguyen
 */
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.util.Observable;

/**
 * Top View is a JPanel that displays the Top dimensions of a brick. 
 * the x value that describes a rectangle in this view is the width of a brick.
 * the y value that describes a rectangle in this view is the length of a brick.
 * @author Duc Nguyen
 *
 */
public class TopView extends BrickView{

	private static final long serialVersionUID = 1L;
	/** opened views count */
	private static int openViewCount = 0;
	/** box displayed in this view */
	private Rectangle2D.Double box;
	
	/**
	 * Constructor
	 * @param model		the model this view refers to
	 */
	public TopView(BrickModel model) {
		
		super(model);
		this.setRectY(model.getLength());
		this.setRectX(model.getWidth());
		this.box = new Rectangle2D.Double(TOP_X, TOP_Y, getRectX(), getRectY());
		
		
	}

	/** paint a rectangle description of the brick */
	public void paintComponent(Graphics gc) {
		super.paintComponent(gc); 
		Graphics2D gc2d = (Graphics2D)gc;
		
	    gc2d.setPaint(Color.red);
	    gc2d.fill(this.box);
	    gc2d.draw(this.box);
	}
	
	/**
	 * to String
	 */
	public String toString(){
		return "TopView #" + (++openViewCount);
	}

	/**
	 * This method determines if pertinent values are changed in the model and updates if necessary
	 */
	@Override
	public void update(Observable arg0, Object arg1) {
		double length = getModel().getLength();
		double width = getModel().getWidth();
		
		if((getRectX() != length)||(getRectY() != width)){
			setRectY(length);
			setRectX(width);
			box = new Rectangle2D.Double(TOP_X, TOP_Y, getRectX(), getRectY());
			
			repaint();
		}
	}
	
	/**
	 * clears the open views count attribute.
	 * Used when files are opened or a new file is created and all current views are destroyed
	 */
    public static void clearViewCount(){
    	openViewCount = 0;
    }
}
